package com.sunday.volunteer.ui.user;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.R;
import com.sunday.volunteer.fragment.member.RakingFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/1/12.
 */

public class RakingActivity extends BaseActivity {

    @Bind(R.id.title_text)
    TextView title_text;
    private String tabTitles[] = {"个人", "组织"};
    private int status[] = {1, 2};
    private RakingFragment[] fragment = {new RakingFragment(), new RakingFragment()};

    @Bind(R.id.tab_layout)
    TabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_raking);
        ButterKnife.bind(this);

        title_text.setText("排行榜");

        viewPager.setAdapter(new TabAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewPager.setCurrentItem(getIntent().getIntExtra("page", 0));
    }


    public class TabAdapter extends FragmentPagerAdapter {
        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return tabTitles.length;
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", status[position]);
            fragment[position].setArguments(bundle);
            return fragment[position];
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }
    }


}
